package com.stats.ultimate;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Edit extends Activity {
	// Declare variables to hold a database helper and SQLiteDatabse variable
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	// Declare variable to hold id
	int id;
	
	// Declare variable to hold number of players
	int num_players;
	
	// Declare EditText field to hold team name
	EditText team_name;
	
	// Declare EditText array to hold player names
	ArrayList<EditText> players = new ArrayList<EditText>();
	
	// Declare EditText array to hold newly created players names
	ArrayList<EditText> new_players = new ArrayList<EditText>();
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set content view to be prepare xml file
        setContentView(R.layout.edit);
        
        // Initialize a new database helper variable
        db_helper = new DatabaseHelper(this);
        
        // Instantiate EditText box
        team_name = (EditText) findViewById(R.id.team_in);
        
        // Instantiate delete_team button
        Button delete_team = (Button) findViewById(R.id.delete);
        
        // Instantiate add button
        Button add = (Button) findViewById(R.id.add_player);
        
        // Instantiate save button
        Button save = (Button) findViewById(R.id.save);
        
        // Instantiate a relative layout that is inside a scollview defined in the xml file
        final RelativeLayout l1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
        
        // Check if savedInstanceState is non-null
        if(savedInstanceState != null){
        	// If so get the id of the team being edited from the previous activity
        	id = savedInstanceState.getInt("id");
        	
        	// Get the previous string in the team_name field and put it back in that field
        	team_name.setText(savedInstanceState.getString("team_name"));
        	
        	// Get the string array for the players array and save it
        	ArrayList<String> names = savedInstanceState.getStringArrayList("states");
        	
        	// Get the string array for the new_players array and save it
        	ArrayList<String> new_names = savedInstanceState.getStringArrayList("new_states");
        	
        	// Check if there was at least 1 editText field left in the players array
        	if(names.size() > 0){
        		// If so create EditText field
        		EditText first_player = new EditText(this);

        		// Set the text of first_player to be whatever was in first box last
        		first_player.setText(names.get(0));

        		// Set the width of first_player to be 150
        		first_player.setWidth(150);

        		// Set the input type of first_player to be a person's name
        		first_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

        		// Define the layout parameters to be used for first_player
        		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        		// Set first_player to be left aligned in parent
        		params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

        		// Set the left margin of first_player to be 10 and the bottom margin to be 5
        		params1.setMargins(10, 0, 0, 5);

        		// Set the layout parameters for first_player
        		first_player.setLayoutParams(params1);

        		// Set first_player id to be 1
        		first_player.setId(1);

        		// Add first_player to l1
        		l1.addView(first_player);

        		// Add first_player to array of players
        		players.add(first_player);

        		// Create a button
        		Button first_delete = new Button(this);

        		// Set the text of first_delete to say delete player
        		first_delete.setText(R.string.delete_player);

        		// Define the layout parameters to be used for this button
        		RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        		// Set first_delete to be bottom aligned with first_player
        		params2.addRule(RelativeLayout.ALIGN_BOTTOM, first_player.getId());

        		// Set first_delete to be to the right of first_player
        		params2.addRule(RelativeLayout.RIGHT_OF, first_player.getId());

        		// Set the left margin of first_delete to be 15
        		params2.setMargins(15, 0, 0, 0);

        		// Set the layout parameters of first_delete
        		first_delete.setLayoutParams(params2);

        		// Set the id of first_delete to be 101
        		first_delete.setId(101);

        		// Add first_delete to l1
        		l1.addView(first_delete);

        		// Set onClickListener for first_delete
        		first_delete.setOnClickListener(new View.OnClickListener() {
        			// Override onClick function
        			@Override
        			public void onClick(View v) {
        				// Open database for reading and writing
        				db = db_helper.getWritableDatabase();

        				// Remove this player from the database
        				if(db_helper.deletePlayer(db, id, 1) < 0){
        					// If this is unsuccessful then give an error message
        					Toast.makeText(getApplicationContext(), "Failed to delete player from team", Toast.LENGTH_SHORT).show();

        					// Close the database
        					db.close();
        				}

        				// Else continue
        				else{
        					// Close the database
        					db.close();

        					// Instantiate intent to restart this activity
        					Intent start_edit = new Intent(getApplicationContext(), Edit.class);

        					// Add team id to start_edit as extra
        					start_edit.putExtra("id", id);

        					// Start activity using start_edit intent
        					startActivity(start_edit);

        					// Finish this activity if the next one is closed
        					finish();
        				}
        			}
        		});
        		
        		// Iterate through each player after the first one
        		for(int i = 2; i <= names.size(); i++){
        			// Save this value of i as player_id
        			final int player_id = i;
        			
            		// Create EditText field
            		EditText temp_player = new EditText(this);

            		// Set the text of this field to be whatever was in it before
            		temp_player.setText(names.get(i-1));

            		// Set the width of this field to be 150
            		temp_player.setWidth(150);

            		// Set the input type of this field to be a person's name
            		temp_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

            		// Define the layout parameters to be used for this field
            		RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
            				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            		// Set this field to be left aligned in parent
            		temp_params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

            		// Set this field to be below the one above it
            		temp_params1.addRule(RelativeLayout.BELOW, players.get(i-2).getId());

            		// Set the left margin of this field to be 10 and the bottom margin to be 5
            		temp_params1.setMargins(10, 0, 0, 5);

            		// Set the layout parameters for this field
            		temp_player.setLayoutParams(temp_params1);

            		// Set this fields id
            		temp_player.setId(i);

            		// Add this field to l1
            		l1.addView(temp_player);

            		// Add this field to array of players
            		players.add(temp_player);

            		// Create a button
            		Button temp_delete = new Button(this);

            		// Set the text of this button to say delete player
            		temp_delete.setText(R.string.delete_player);

            		// Define the layout parameters to be used for this button
            		RelativeLayout.LayoutParams temp_params2 = new RelativeLayout.LayoutParams(
            				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            		// Set this button to be bottom aligned with its corresponding field
            		temp_params2.addRule(RelativeLayout.ALIGN_BOTTOM, temp_player.getId());

            		// Set this button to be to the right of its corresponding field
            		temp_params2.addRule(RelativeLayout.RIGHT_OF, temp_player.getId());

            		// Set the left margin of this button to be 15
            		temp_params2.setMargins(15, 0, 0, 0);

            		// Set the layout parameters of this button
            		temp_delete.setLayoutParams(temp_params2);

            		// Set the id of this button
            		temp_delete.setId(100 + i);

            		// Add this button to l1
            		l1.addView(temp_delete);

            		// Set onClickListener for this button
            		temp_delete.setOnClickListener(new View.OnClickListener() {
            			// Override onClick function
            			@Override
            			public void onClick(View v) {
            				// Open database for reading and writing
            				db = db_helper.getWritableDatabase();

            				// Remove this player from the database
            				if(db_helper.deletePlayer(db, id, player_id) < 0){
            					// If this is unsuccessful then give an error message
            					Toast.makeText(getApplicationContext(), "Failed to delete player from team", Toast.LENGTH_SHORT).show();

            					// Close the database
            					db.close();
            				}

            				// Else continue
            				else{
            					// Close the database
            					db.close();

            					// Instantiate intent to restart this activity
            					Intent start_edit = new Intent(getApplicationContext(), Edit.class);

            					// Add team id to start_edit as extra
            					start_edit.putExtra("id", id);

            					// Start activity using start_edit intent
            					startActivity(start_edit);

            					// Finish this activity if the next one is closed
            					finish();
            				}
            			}
            		});
        		}
        		
        		// Set the number of players to be how many players were in players array
        		num_players = names.size();
        		
        		// Iterate through each element in the new_player array
        		for(int i = 0; i < new_names.size(); i++){
    				// Create EditText field
    	            EditText temp_player = new EditText(getApplicationContext());
    	            
    	            // Set the text of this field to be what it was before
    	            temp_player.setText(new_names.get(i));
    	            
    	            // Set the width of this field to be 150
    	            temp_player.setWidth(150);
    	            
    	            // Set the input type of this field to be a person's name
    	            temp_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
    	            
    	            // Define the layout parameters to be used for this field
    	            RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
    	            		RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    	            
    	            // Set this field to be left aligned in parent
    	            temp_params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    	            
    	            // Set this field to be below the one above it
    	            temp_params1.addRule(RelativeLayout.BELOW, num_players);
    	            
    	            // Set the left margin of this field to be 10 and the bottom margin to be 5
    	            temp_params1.setMargins(10, 0, 0, 5);
    	            
    	            // Set the layout parameters for this field
    	            temp_player.setLayoutParams(temp_params1);
    	            
    	            // Set this fields id
    	            temp_player.setId(++num_players);
    	            
    	            // Add this field to l1
    	            l1.addView(temp_player);
    	            
    	            // Add edit_player_name to new_players array
    	            new_players.add(temp_player);
        		}
        	}
        	
        	// Else check if there was at least 1 editText field in the new_players array
        	else if(new_names.size() > 0){
        		// If so initialize num_players to 1
        		num_players = 1;
        		
        		// Create EditText field
        		EditText first_player = new EditText(this);

        		// Set the text of first_player to be whatever was in first box last
        		first_player.setText(names.get(0));

        		// Set the width of first_player to be 150
        		first_player.setWidth(150);

        		// Set the input type of first_player to be a person's name
        		first_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

        		// Set the text in this field to be what it was before
        		first_player.setText(new_names.get(0));
        		
        		// Define the layout parameters to be used for first_player
        		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        		// Set first_player to be left aligned in parent
        		params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

        		// Set the left margin of first_player to be 10 and the bottom margin to be 5
        		params1.setMargins(10, 0, 0, 5);

        		// Set the layout parameters for first_player
        		first_player.setLayoutParams(params1);

        		// Set first_player id to be 1
        		first_player.setId(1);

        		// Add first_player to l1
        		l1.addView(first_player);

        		// Add first_player to array of players
        		new_players.add(first_player);
        		
        		// Iterate through all new players after the first one
        		for(int i = 1; i < new_players.size(); i++){
        			// Create EditText field
    	            EditText temp_player = new EditText(getApplicationContext());
    	            
    	            // Set the text of this field to be what it was before
    	            temp_player.setText(new_names.get(i));
    	            
    	            // Set the width of this field to be 150
    	            temp_player.setWidth(150);
    	            
    	            // Set the input type of this field to be a person's name
    	            temp_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
    	            
    	            // Set the text of this field to be what it was before
    	            temp_player.setText(new_names.get(i));
    	            
    	            // Define the layout parameters to be used for this field
    	            RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
    	            		RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    	            
    	            // Set this field to be left aligned in parent
    	            temp_params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    	            
    	            // Set this field to be below the one above it
    	            temp_params1.addRule(RelativeLayout.BELOW, num_players);
    	            
    	            // Set the left margin of this field to be 10 and the bottom margin to be 5
    	            temp_params1.setMargins(10, 0, 0, 5);
    	            
    	            // Set the layout parameters for this field
    	            temp_player.setLayoutParams(temp_params1);
    	            
    	            // Set this fields id
    	            temp_player.setId(++num_players);
    	            
    	            // Add this field to l1
    	            l1.addView(temp_player);
    	            
    	            // Add edit_player_name to new_players array
    	            new_players.add(temp_player);
        		}
        	}
        }
        
        // Else get string passed by previous activity
        else{
        	// Get user id and save it to id
        	id = this.getIntent().getIntExtra("id", 1);
        
        	// Open a database for reading and writing
        	db = db_helper.getWritableDatabase();

        	// Get team name
        	String name = db_helper.getTeamName(db, id);

        	// Set the text of the team name to be the current name
        	team_name.setText(name);

        	// Get number of players
        	num_players = db_helper.getNumberPlayers(db, id);

        	// Close the database
        	db.close();

        	// Check if this team has at least 1 player
        	if(num_players > 0){
        		// If so reopen the database
        		db = db_helper.getWritableDatabase();

        		// Get first players name
        		String first_name = db_helper.getPlayerName(db, id, 1);

        		// Close the database
        		db.close();

        		// Create EditText field
        		EditText first_player = new EditText(this);

        		// Set the text of first_player to be first_name
        		first_player.setText(first_name);

        		// Set the width of first_player to be 150
        		first_player.setWidth(150);

        		// Set the input type of first_player to be a person's name
        		first_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

        		// Define the layout parameters to be used for first_player
        		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        		// Set first_player to be left aligned in parent
        		params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

        		// Set the left margin of first_player to be 10 and the bottom margin to be 5
        		params1.setMargins(10, 0, 0, 5);

        		// Set the layout parameters for first_player
        		first_player.setLayoutParams(params1);

        		// Set first_player id to be 1
        		first_player.setId(1);

        		// Add first_player to l1
        		l1.addView(first_player);

        		// Add first_player to array of players
        		players.add(first_player);

        		// Create a button
        		Button first_delete = new Button(this);

        		// Set the text of first_delete to say delete player
        		first_delete.setText(R.string.delete_player);

        		// Define the layout parameters to be used for this button
        		RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        		// Set first_delete to be bottom aligned with first_player
        		params2.addRule(RelativeLayout.ALIGN_BOTTOM, first_player.getId());

        		// Set first_delete to be to the right of first_player
        		params2.addRule(RelativeLayout.RIGHT_OF, first_player.getId());

        		// Set the left margin of first_delete to be 15
        		params2.setMargins(15, 0, 0, 0);

        		// Set the layout parameters of first_delete
        		first_delete.setLayoutParams(params2);

        		// Set the id of first_delete to be 101
        		first_delete.setId(101);

        		// Add first_delete to l1
        		l1.addView(first_delete);

        		// Set onClickListener for first_delete
        		first_delete.setOnClickListener(new View.OnClickListener() {
        			// Override onClick function
        			@Override
        			public void onClick(View v) {
        				// Open database for reading and writing
        				db = db_helper.getWritableDatabase();

        				// Remove this player from the database
        				if(db_helper.deletePlayer(db, id, 1) < 0){
        					// If this is unsuccessful then give an error message
        					Toast.makeText(getApplicationContext(), "Failed to delete player from team", Toast.LENGTH_SHORT).show();

        					// Close the database
        					db.close();
        				}

        				// Else continue
        				else{
        					// Close the database
        					db.close();

        					// Instantiate intent to restart this activity
        					Intent start_edit = new Intent(getApplicationContext(), Edit.class);

        					// Add team id to start_edit as extra
        					start_edit.putExtra("id", id);

        					// Start activity using start_edit intent
        					startActivity(start_edit);

        					// Finish this activity if the next one is closed
        					finish();
        				}
        			}
        		});
        	}

        	// Iterate through each player after first one
        	for(int i = 2; i <= num_players; i++){
        		// Save this value of i as player id
        		final int player_id = i;

        		// Open the database
        		db = db_helper.getWritableDatabase();

        		// Get this players name
        		String player_name = db_helper.getPlayerName(db, id, player_id);

        		// Close the database
        		db.close();

        		// Create EditText field
        		EditText temp_player = new EditText(this);

        		// Set the text of this field to be this players name
        		temp_player.setText(player_name);

        		// Set the width of this field to be 150
        		temp_player.setWidth(150);

        		// Set the input type of this field to be a person's name
        		temp_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

        		// Define the layout parameters to be used for this field
        		RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        		// Set this field to be left aligned in parent
        		temp_params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

        		// Set this field to be below the one above it
        		temp_params1.addRule(RelativeLayout.BELOW, players.get(i-2).getId());

        		// Set the left margin of this field to be 10 and the bottom margin to be 5
        		temp_params1.setMargins(10, 0, 0, 5);

        		// Set the layout parameters for this field
        		temp_player.setLayoutParams(temp_params1);

        		// Set this fields id
        		temp_player.setId(i);

        		// Add this field to l1
        		l1.addView(temp_player);

        		// Add this field to array of players
        		players.add(temp_player);

        		// Create a button
        		Button temp_delete = new Button(this);

        		// Set the text of this button to say delete player
        		temp_delete.setText(R.string.delete_player);

        		// Define the layout parameters to be used for this button
        		RelativeLayout.LayoutParams temp_params2 = new RelativeLayout.LayoutParams(
        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        		// Set this button to be bottom aligned with its corresponding field
        		temp_params2.addRule(RelativeLayout.ALIGN_BOTTOM, temp_player.getId());

        		// Set this button to be to the right of its corresponding field
        		temp_params2.addRule(RelativeLayout.RIGHT_OF, temp_player.getId());

        		// Set the left margin of this button to be 15
        		temp_params2.setMargins(15, 0, 0, 0);

        		// Set the layout parameters of this button
        		temp_delete.setLayoutParams(temp_params2);

        		// Set the id of this button
        		temp_delete.setId(100 + i);

        		// Add this button to l1
        		l1.addView(temp_delete);

        		// Set onClickListener for this button
        		temp_delete.setOnClickListener(new View.OnClickListener() {
        			// Override onClick function
        			@Override
        			public void onClick(View v) {
        				// Open database for reading and writing
        				db = db_helper.getWritableDatabase();

        				// Remove this player from the database
        				if(db_helper.deletePlayer(db, id, player_id) < 0){
        					// If this is unsuccessful then give an error message
        					Toast.makeText(getApplicationContext(), "Failed to delete player from team", Toast.LENGTH_SHORT).show();

        					// Close the database
        					db.close();
        				}

        				// Else continue
        				else{
        					// Close the database
        					db.close();

        					// Instantiate intent to restart this activity
        					Intent start_edit = new Intent(getApplicationContext(), Edit.class);

        					// Add team id to start_edit as extra
        					start_edit.putExtra("id", id);

        					// Start activity using start_edit intent
        					startActivity(start_edit);

        					// Finish this activity if the next one is closed
        					finish();
        				}
        			}
        		});
        	}
        }
        
        // Set onClickListener for delete button
        delete_team.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Open database for reading and writing
				db = db_helper.getWritableDatabase();
				
				// Remove this team from the database
				db_helper.deleteTeam(db, id);
				
				// Close database
				db.close();
				
				// Finish this activity
				finish();
			}
		});
        
        // Set onClickListener for new_player button
        add.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Check if num_players is 0 
				if(num_players == 0){
	        		// If so create EditText field and make it the top thing in the scroll view
	        		EditText temp_player = new EditText(getApplicationContext());

	        		// Set the width of first_player to be 150
	        		temp_player.setWidth(150);

	        		// Set the input type of first_player to be a person's name
	        		temp_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

	        		// Define the layout parameters to be used for first_player
	        		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
	        				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

	        		// Set first_player to be left aligned in parent
	        		params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

	        		// Set the left margin of first_player to be 10 and the bottom margin to be 5
	        		params1.setMargins(10, 0, 0, 5);

	        		// Set the layout parameters for first_player
	        		temp_player.setLayoutParams(params1);

	        		// Set first_player id to be 1
	        		temp_player.setId(1);

	        		// Add first_player to l1
	        		l1.addView(temp_player);

	        		// Add first_player to array of players
	        		new_players.add(temp_player);	
				}
				
				// Else there are already things in the scroll view
				else{
					// Create EditText field
					EditText temp_player = new EditText(getApplicationContext());

					// Set the width of this field to be 150
					temp_player.setWidth(150);

					// Set the input type of this field to be a person's name
					temp_player.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);

					// Define the layout parameters to be used for this field
					RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
							RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

					// Set this field to be left aligned in parent
					temp_params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

					// Set this field to be below the one above it
					temp_params1.addRule(RelativeLayout.BELOW, num_players);

					// Set the left margin of this field to be 10 and the bottom margin to be 5
					temp_params1.setMargins(10, 0, 0, 5);

					// Set the layout parameters for this field
					temp_player.setLayoutParams(temp_params1);

					// Set this fields id
					temp_player.setId(++num_players);

					// Add this field to l1
					l1.addView(temp_player);

					// Add edit_player_name to new_players array
					new_players.add(temp_player);
				}
			}
		});
        
        // Set onClickListener for save button
        save.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Get team's name from team_name
				String team = team_name.getText().toString();
				
				// Create array to hold players names
				ArrayList<String> names = new ArrayList<String>();
				
				// Create array to hold new_players names
				ArrayList<String> new_names = new ArrayList<String>();
				
				// for loop to increment through players array
				for(int i = 0; i < players.size(); i++){
					// Get this players name and save it into names array
					names.add(players.get(i).getText().toString());
				}
				
				// for loop to increment through new_players array
				for(int i = 0; i < new_players.size(); i++){
					// Get this players name and save it into new_names array
					new_names.add(new_players.get(i).getText().toString());
				}
				
				// Check if given team_in is blank
				if(team.equalsIgnoreCase("")){
					// If so give message and don't create team
					Toast.makeText(getApplicationContext(), "Team name cannot be blank", Toast.LENGTH_SHORT).show();
				}
				
				// Else continue
				else{
					// Initialize blank to false
					boolean blank = false;
					
					// for loop to increment through names array
					for(int i = 0; i < names.size(); i++){
						// Check if this name is blank
						if(names.get(i).toString().equalsIgnoreCase("")){
							// If so set blank to true and break for loop
							blank = true;
							break;
						}
					}
					
					// Check if blank is still false
					if(!blank){
						// If so for loop to increment through new_names array
						for(int i = 0; i < new_names.size(); i++){
							// Check if this name is blank
							if(new_names.get(i).equalsIgnoreCase("")){
								// If so set blank to true and break for loop
								blank = true;
								break;
							}
						}
					}
					
					// Check if blank is now true
					if(blank){
						// If so give a message and do not create team
						Toast.makeText(getApplicationContext(), "Players' names cannot be blank" , Toast.LENGTH_SHORT).show();
					}
					
					// Else continue
					else{
						// Initialize identical to false
						boolean identical = false;
						
						// for loop to increment through all players' names but last one
						for(int i = 0; i < names.size() - 1; i++){
							// for loop to increment through all players' names after this one
							for(int j = i+1; j < names.size(); j++){
								// Check if these two players' names are identical
								if(names.get(i).equalsIgnoreCase(names.get(j))){
									// If so set identical to true and break for loop
									identical = true;
									break;
								}
							}
							
							// Check if identical is true
							if(identical){
								// If so break outer for loop as well
								break;
							}
							
							// for loop to increment through all new players
							for(int j = 0; j < new_names.size(); j++){
								// Check if these two players' names are identical
								if(names.get(i).equalsIgnoreCase(new_names.get(j))){
									// If so set identical to true and break for loop
									identical = true;
									break;
        						}
							}
							
							// Check if identical is true
							if(identical){
								// If so breka outer for loop as well
								break;
							}
						}
						
						// Check if identical is still false
						if(!identical){
							// If so for loop to increment through all new players' names but last one
							for(int i = 0; i < new_names.size() - 1; i++){
								// for loop to increment through all new_players' names after this one
								for(int j = i+1; j< new_names.size(); j++){
									// Check if these two names are identical
									if(new_names.get(i).equalsIgnoreCase(new_names.get(j))){
										// If so set indentical to true and break the loop
										identical = true;
										break;
									}
								}

								// Check if identical is true
								if(identical){
									// If so break outer for loop as well
									break;
								}
							}
						}
						
						// Check if identical is true
						if(identical){
							// If so then give an error message
							Toast.makeText(getApplicationContext(), "Cannot have identical player names", Toast.LENGTH_SHORT).show();
						}
						
						// Else it was false so continue
						else{
							// If so open database for reading and writing
							db = db_helper.getWritableDatabase();

							// Call update team function with given team name and players' names
							if(db_helper.updateTeam(db, id, team, names, new_names) < 0){
								// If unsuccessful give a notification of failure
								Toast.makeText(getApplicationContext(), "Failed to update team", Toast.LENGTH_SHORT).show();
							}
							
							// Close database
							db.close();
							
							// Finish this activity
							finish();
						}
					}
				}
			}
		});
    }
    
    // Override onSavedInstanceState function
    @Override
    public void onSaveInstanceState(Bundle outState){
    	// Call super function
    	super.onSaveInstanceState(outState);
    	
    	// Put id into outState bundle
    	outState.putInt("id", id);
    	
    	// Put whatever text is in team_name into outState bundle
    	outState.putString("team_name", team_name.getText().toString());
    	
    	// Initialize a string array to hold input values for editText boxes in players array
    	ArrayList<String> states = new ArrayList<String>();
    	
    	// Iterate through players array 
    	for(int i = 0; i < players.size(); i++){
    		// Add whatever is in this editText field into states array
    		states.add(players.get(i).getText().toString());
    	}
    	
    	// Put states array into outState bundle
    	outState.putStringArrayList("states", states);
    	
    	// Initialize a string array to hold input values for editText boxes in new_players array
    	ArrayList<String> new_states = new ArrayList<String>();
    	
    	// Iterate through new_players array
    	for(int i = 0; i < new_players.size(); i++){
    		// Add whatever is in this editText field into new_states array
    		new_states.add(new_players.get(i).getText().toString());
    	}
    	
    	// Put new_states array into outState bundle
    	outState.putStringArrayList("new_states", new_states);
    }
}
